package com.seari.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.DataGridBean;
import com.seari.bean.QueryProBean;
import com.seari.bean.SelectObj;
import com.seari.main.StartUpClass;
import com.seari.pojo.InvestHoleInPojo;
import com.seari.pojo.ProNodePojo;
import com.seari.service.ProSearchService;
import com.seari.service.ProgrammeService;

/**
 * @author TOUC 子页面节点、投资数、资金数、形象进度
 *
 */
@Controller
@RequestMapping("/prodetail")
public class ProgrammeController {

	@Autowired
	private ProgrammeService programmeService;
	@Autowired
	private ProSearchService proSearchService;

	/**
	 * @return 节点关键路径
	 */
	@RequestMapping("proCruxNode")
	@ResponseBody
	public DataGridBean<SelectObj> proCruxNode(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		if (StartUpClass.proCruxNode.equals("pms")) {
			String proid = null;
			try {
				proid = queryProBean.getProId();
			} catch (Exception e) {

			}
			if (proid == null) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else {
				// 附属合同
				List<SelectObj> conLabel = programmeService
						.queryConByProid(queryProBean);
				// 计划关键路径
				List<ProNodePojo> ProNodePlanList = programmeService
						.queryProNodePlan(queryProBean);
				// 实际关键路径
				List<ProNodePojo> ProNodeActualList = programmeService
						.queryProNodeActual(queryProBean);
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("conLabel", conLabel);
				dataMap.put("ProNodePlan", ProNodePlanList);
				dataMap.put("ProNodeActual", ProNodeActualList);

				grid.setData(dataMap);
				grid.setCode("succee");
			}
		} else {

		}
		return grid;
	}

	/**
	 * @return 节点附属路径
	 */
	@RequestMapping("proMinorNode")
	@ResponseBody
	public DataGridBean<SelectObj> proMinorNode(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();
		Map<String, Object> dataMap = new HashMap<String, Object>();

		if (StartUpClass.proMinorNode.equals("pms")) {
			List<String> conIdList = null;
			try {
				conIdList = queryProBean.getConIdList();
			} catch (Exception e) {

			}
			if (conIdList.size() == 0) {
				grid.setCode("err");
				grid.setMsg("合同编号不能为空!");
				return grid;
			} else {
				// 查询偏差说明 改进措施
				List<Map<String, String>> emList = programmeService.queryExplainMeasuresByconid(queryProBean.getMileId());
				dataMap.put("ExplainMeasures", emList);
				
				if(queryProBean.getStatu().equals("0")) {
					grid.setCode("succee");
					return grid ;
					
				}else {

				// 计划附属路径
				List<ProNodePojo> MinorPlanList = programmeService
						.queryProMinorNodePlan(queryProBean);
				// 实际附属路径
				List<ProNodePojo> MinorActualList = programmeService
						.queryProMinorNodeActual(queryProBean);

				dataMap.put("MinorPlan", MinorPlanList);
				dataMap.put("MinorActual", MinorActualList);
				}
				grid.setCode("succee");
				grid.setData(dataMap);
				
			}
		} else {

		}

		return grid;
	}

	/**
	 * @param proText
	 * @return 项目投资数
	 */
	@RequestMapping("proInvest")
	@ResponseBody
	public DataGridBean<SelectObj> proInvest(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();
		if (StartUpClass.proInvest.equals("pms")) {
			String proid = null;
			try {
				proid = queryProBean.getProId();
			} catch (Exception e) {
			}
			if (proid == null) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else {
				List<String> proIdList = new ArrayList<String>();
				proIdList.add(proid);
				queryProBean.setProIdList(proIdList);
				List<String> conIdList = proSearchService
						.getConIdsByproIds(queryProBean);
				queryProBean.setConIdList(conIdList);
				List<Map<String, String>> proInvest = programmeService.getProInvest(queryProBean);
				if (proInvest.size() > 0) {
					grid.setData(proInvest);
					grid.setCode("succee");
				}
			}
		} else if (StartUpClass.proInvest.equals("sap")) {
			String ebeln=queryProBean.getProId();
			String year=queryProBean.getYear();
			int m=Integer.parseInt(queryProBean.getMonth());
			String time = m>=10?year+m:year+"0"+m;

			Map<String,Object> result=new HashMap<>();
			Map<String,String> nowMap=programmeService.queryPlanAcRatio(ebeln,time,time);//当月
			if(nowMap!=null) {
				result.put("monthRatio", String.valueOf(nowMap.get("ratio")));
				result.put("monthInvestPlan", nowMap.get("pl"));
				result.put("monthInvestAc", nowMap.get("ac"));
			}
			Map<String,String> zyMap=programmeService.queryPlanAcRatio(ebeln,year+"01",time);//逐月
			if(zyMap!=null) {
				result.put("yearRatio", String.valueOf(zyMap.get("ratio")));
				result.put("yearInvestPlan", zyMap.get("pl"));
				result.put("yearInvestAc", zyMap.get("ac"));
			}
			Map<String,String> qnMap=programmeService.queryPlanAcRatio(ebeln,year+"01",year+"12");//全年
			if(qnMap!=null) {
				result.put("annualRatio", String.valueOf(qnMap.get("ratio")));
				result.put("annualInvestPlan", qnMap.get("pl"));
				result.put("annualInvestAc", qnMap.get("ac"));
			}
			Map<String,String> proMap=programmeService.queryPlanAcRatio(ebeln,"","");//项目
			if(proMap!=null) {
				result.put("addupRatio", String.valueOf(proMap.get("ratio")));
				result.put("addupInvestPlan", proMap.get("pl"));
				result.put("addupInvestAc", proMap.get("ac"));
			}

			grid.setData(result);
			grid.setCode("succee");
		}
		return grid;
	}

	/**
	 * @param
	 * @return 投资数下钻功能
	 */
	@RequestMapping("proInvestDown")
	@ResponseBody
	public DataGridBean<SelectObj> proInvestDown(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		if (StartUpClass.proInvestDown.equals("pms")) {
			String proid = queryProBean.getProId();
			String year=queryProBean.getYear();
			int m=Integer.parseInt(queryProBean.getMonth());
			String time = m>=10?year+m:year+"0"+m;
			if (StringUtils.isBlank(proid)) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getDiscern())) {
				grid.setCode("err");
				grid.setMsg("属性不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getYear())) {
				grid.setCode("err");
				grid.setMsg("年份不能为空!");
				return grid;
			}
			// 获取项目下所有合同
			List<String> proIdList = new ArrayList<String>();
			proIdList.add(proid);
			queryProBean.setProIdList(proIdList);
			List<String> conIdList = proSearchService
					.getConIdsByproIds(queryProBean);
			queryProBean.setConIdList(conIdList);

			List<InvestHoleInPojo> investHoleIn = programmeService
					.getInvestHoleIn(queryProBean);

			grid.setData(investHoleIn);
			grid.setCode("succee");

		} else if (StartUpClass.proInvestDown.equals("sap")) {
			String ebeln = queryProBean.getProId();
			String flag=queryProBean.getDiscern();
			String year=queryProBean.getYear();
			int m=Integer.parseInt(queryProBean.getMonth());
			String time = m>=10?year+m:year+"0"+m;
			if("month".equals(flag)){
				grid.setData(programmeService.queryPlanAcDetails(ebeln,time,time));
			}else if("accu".equals(flag)){
				grid.setData(programmeService.queryPlanAcDetails(ebeln,year+"01",time));
			}else if("year".equals(flag)){
				grid.setData(programmeService.queryPlanAcDetails(ebeln,year+"01",year+"12"));
			}
			grid.setCode("succee");
		}
		return grid;
	}

	/**
	 * @return 投资数偏差说明及改进措施
	 */
	@RequestMapping("proInvertExplain")
	@ResponseBody
	public DataGridBean<SelectObj> proInvertExplain(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		if (StartUpClass.proInvertExplain.equals("pms")) {
			String proid = queryProBean.getProId();
			if (StringUtils.isBlank(proid)) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getMonth())) {
				grid.setCode("err");
				grid.setMsg("月份不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getYear())) {
				grid.setCode("err");
				grid.setMsg("年份不能为空!");
				return grid;
			}
		} else if (StartUpClass.proInvertExplain.equals("sap")) {

		}
		return grid;
	}

	/**
	 * @return 项目资金数
	 */
	@RequestMapping("moneyNum")
	@ResponseBody
	public DataGridBean<SelectObj> proMoneyNum(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		if (StartUpClass.moneyNum.equals("pms")) {
			String proid = null;
			try {
				proid = queryProBean.getProId();
			} catch (Exception e) {

			}
			if (proid == null) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else {

				List<Map<String, String>> proInvest = programmeService
						.getProMoneyNum(queryProBean);

				if (proInvest.size() > 0) {
					grid.setData(proInvest);
					grid.setCode("succee");
				}
			}
		} else if (StartUpClass.moneyNum.equals("sap")) {

		}
		return grid;
	}

	/**
	 * @return 资金数下钻功能
	 */
	@RequestMapping("proMoneyNumDown")
	@ResponseBody
	public DataGridBean<SelectObj> proMoneyNumDown(
			@RequestBody(required = false) QueryProBean queryProBean) {
		DataGridBean<SelectObj> grid = new DataGridBean<SelectObj>();

		if (StartUpClass.proMoneyNumDown.equals("pms")) {
			String proid = queryProBean.getProId();
			if (StringUtils.isBlank(proid)) {
				grid.setCode("err");
				grid.setMsg("项目编号不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getDiscern())) {
				grid.setCode("err");
				grid.setMsg("属性不能为空!");
				return grid;
			} else if (StringUtils.isBlank(queryProBean.getYear())) {
				grid.setCode("err");
				grid.setMsg("年份不能为空!");
				return grid;
			}

			// 获取项目下所有合同
			List<String> proIdList = new ArrayList<String>();
			proIdList.add(proid);
			queryProBean.setProIdList(proIdList);
			List<String> conIdList = proSearchService
					.getConIdsByproIds(queryProBean);
			queryProBean.setConIdList(conIdList);

			List<InvestHoleInPojo> moneyNum = programmeService
					.getProMoneyNumDown(queryProBean);

			grid.setData(moneyNum);
			grid.setCode("succee");

		} else if (StartUpClass.proMoneyNumDown.equals("sap")) {

		}
		return grid;
	}

}
